package com.aode.music.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.aode.common.bean.request.LoginData;
import com.aode.common.bean.response.User;
import com.aode.common.bean.web.Result;
import com.aode.music.R;
import com.aode.music.databinding.ActivityLoginBinding;
import com.aode.music.viewmodel.LoginViewModel;
import com.aode.utils.main.ToastUtils;
import com.aode.utils.sharedhelper.SharedHelper;

public class LoginActivity extends AppCompatActivity {
    private ActivityLoginBinding loginBinding;
    private LoginViewModel loginViewModel;
    private String username;
    private String password;
    private LoginData loginData = new LoginData();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        loginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
        loginBinding.setLogin(LoginActivity.this);
    }



    private void initListener() {
        loginViewModel.getLogin().observe(this, userResult -> {
            Log.e("TAG",userResult.getCode());
            if(userResult.getCode().equals("0")){
                if(userResult.getData()!=null){
                    ToastUtils.show(getApplicationContext(),"登录成功");
                    Intent intent = new Intent();
                    intent.setClass(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    SharedHelper sharedHelper = new SharedHelper(getApplicationContext());
                    sharedHelper.save(userResult.getData().getUserId()+"");
                    finish();
                }else{
                    ToastUtils.show(getApplicationContext(),"用户名或密码错误");
                }
            }else{
                Log.e("TAG->>>",userResult.getMsg());
                ToastUtils.show(getApplicationContext(),userResult.getMsg());
            }
        });
    }
    private boolean checkNetwork() {
        ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if(networkInfo!=null){
            return  true;
        }else{
            return false;
        }
    }
    @SuppressLint("NonConstantResourceId")
    public void click(View view){
        switch (view.getId()){
            case R.id.buttonSignIn:
                boolean b = checkNetwork();
                if(b){
                    boolean a = checkLogin();
                    login(a);
                }else{
                    ToastUtils.show(getApplicationContext(),"请接查网络");
                }

                break;
            case R.id.textCreateNewAccount:
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private boolean checkLogin() {
        username = loginBinding.inputName.getText().toString().trim();
        password = loginBinding.inputPassword.getText().toString().trim();
        if(username.equals("")){
            ToastUtils.show(LoginActivity.this,"用户名不能为空");
            return false;
        }else if(password.equals("")){
            ToastUtils.show(LoginActivity.this,"密码不能为空");
            return false;
        }
        return true;
    }

    private void login(boolean a) {
        if(a){
            loginData.setUsername(username);
            loginData.setPassword(password);
            loginViewModel.Login(loginData);
            initListener();
        }
    }
}
